//
//  UIButton+Remind.m
//  us
//
//  Created by lhx on 2025/8/16.
//

#import "UIButton+Remind.h"

@implementation UIButton (Remind)

+ (UIButton *)us_remindButtonWithTitle:(NSString *)title{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage jk_imageWithColor:UIColor.titleColor] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage jk_imageWithColor:[UIColor jk_colorWithHex:0x5C5C5C]] forState:UIControlStateDisabled];
    [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [button setTitleColor:[UIColor whiteColor] forState:0];
    button.layer.cornerRadius = 14;
    button.clipsToBounds = YES;
    
//    [button addTarget:button action:@selector(us_handleRemindTap:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}


// 提供一个方法，让外部追加行为
- (void)us_addRemindCustomAction:(void(^)(UIButton *sender))block {
    objc_setAssociatedObject(self, @"USRemindDefaultActionKey", block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(us_handleRemindTap:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)us_handleRemindTap:(UIButton *)sender {
    [USFeedbackTool impactSoft];

    sender.enabled = NO; // 防止重复点击
    
    // 1. 按钮缩放动画（按压反馈）
    [UIView animateWithDuration:0.15 animations:^{
        sender.transform = CGAffineTransformMakeScale(0.9, 0.9);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.15 animations:^{
            sender.transform = CGAffineTransformIdentity;
        }];
    }];
    
    // 2. 显示心形动画
    [self showHeartAnimationAboveButton:sender];
    
    // 3. 切换按钮状态
    [sender setTitle:@"已提醒" forState:UIControlStateNormal];
    sender.backgroundColor = [UIColor lightGrayColor];
    
    // 额外执行用户自定义的回调（如果有）
    void (^customBlock)(UIButton *) = objc_getAssociatedObject(self, @"USRemindDefaultActionKey");
    if (customBlock) {
        customBlock(sender);
    }
}

// 显示心形动画
- (void)showHeartAnimationAboveButton:(UIButton *)button {
    UIImageView *heartView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_love_red"]];
    heartView.tintColor = [UIColor systemPinkColor];
    heartView.frame = CGRectMake(0, 0, 24, 24);
    
    // 将心形放在按钮上方
    CGPoint startPoint = CGPointMake(button.center.x, button.frame.origin.y);
    heartView.center = startPoint;
    [self.superview addSubview:heartView];
    
    // 目标点（往上飘 80px）
    CGPoint endPoint = CGPointMake(startPoint.x, startPoint.y - 80);
    
    [UIView animateWithDuration:1.0 animations:^{
        heartView.center = endPoint;
        heartView.alpha = 0;
        heartView.transform = CGAffineTransformMakeScale(1.8, 1.8);
    } completion:^(BOOL finished) {
        [heartView removeFromSuperview];
    }];
}

@end
